<template>
	<view>
		<uni-nav-bar :statusBar="true" dark title="球队详情" fixed left-icon="left" @clickLeft="clickLeft">
		</uni-nav-bar>

		<view class="header_box">
			<image v-if="dataInfo.teamLogo" :src="imageUrl+dataInfo.teamLogo" mode=""></image>
			<image v-else src="@/static/user.jpg" mode=""></image>
			<view>
				<text>{{dataInfo.teamName}}</text>
			</view>
		</view>

		<uni-section class="coachList" title="教练组成员" type="line" titleFontSize="32rpx">
			<template v-slot:right>
				<view class="flex align-center addcoach" @click="addcoach">
					<uni-icons color="#fff" fontFamily="CustomFont" type="plusempty" :size="14"></uni-icons>
					添加教练
				</view>
			</template>
			<view class="coachListData">
				<uni-collapse accordion @change="change">
					<uni-collapse-item thumb="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png"
						:title="item.name" v-for="(item,index) in coachListData">
						<block slot="title">
							<view class="contentList">

								<image :src="imageUrl + item.coachPhoto" mode=""></image>
								<view>
									<view>{{item.name}}</view>
									<text>角色类型：{{item.typeStr}}</text>
								</view>
							</view>
						</block>
						<view class="contentBtn">
							<button @click="putInfo(item)">编辑信息</button>
							<view></view>
							<button @click="onDelecoach(item)">删除</button>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
		</uni-section>
		<!-- 球员 -->
		<uni-section class="coachList" title="球员" type="line" titleFontSize="32rpx">
			<template v-slot:right>
				<view class="flex align-center addcoach" @click="addplayer">
					<uni-icons color="#fff" fontFamily="CustomFont" type="plusempty" :size="14"></uni-icons>
					添加球员
				</view>
			</template>
			<view class="coachListData">
				<uni-collapse accordion @change="change">
					<uni-collapse-item thumb="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png"
						:title="item.name" v-for="(item,index) in playerList">
						<block slot="title">
							<view class="contentList">
								<image v-if="!item.profilePicture" src="@/static/user.jpg" alt="" />
								<image v-else :src="imageUrl + item.profilePicture" mode=""></image>
								<view>
									<view>{{item.name}}</view>
									<text class="num">{{item.playerNumber}}号</text>
								</view>
							</view>
						</block>
						<view class="contentBtn">
							<button @click="putInfoplayer(item)">编辑信息</button>
							<view></view>
							<button @click="delputInfo(item)">删除</button>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
		</uni-section>

	</view>
</template>

<script>
	import {
		getInfoBasketballTeam,
		getCoachList,
		getbasketballPlayerList,
		delcoach,
		delbasketballPlayer
	} from '@/api/basketballTeam/index.js'
	import {
		imageUrl
	} from '@/config.js'
	export default {
		data() {
			return {
				imageUrl: imageUrl,
				id: '',
				dataInfo: {

				},
				coachListData: [],
				playerList: [],
				loadingMore: false,
				pageNum: 1,
				isLast: false,
			}
		},
		onReachBottom() {
			console.log(1111, this.pageNum)
			this.pageNum++
			if (!this.loadingMore && !this.isLast) {
				this.basketballPlayerList();
			}
		},
		methods: {
			clickLeft() {
				uni.navigateBack()
			},
			addcoach() {
				uni.navigateTo({
					url: `/pages/basketballTeam/createcoach?player=${this.dataInfo.id}&url=${imageUrl+this.dataInfo.teamLogo}&name=${this.dataInfo.teamName}`
				})
			},
			putInfo(item) {
				uni.navigateTo({
					url: `/pages/basketballTeam/createcoach?player=${this.dataInfo.id}&type=put&id=${item.id}&url=${imageUrl+this.dataInfo.teamLogo}&name=${this.dataInfo.teamName}`
				})
			},
			putInfoplayer(item) {
				uni.navigateTo({
					url: `/pages/basketballTeam/addplayer?player=${this.dataInfo.id}&type=put&id=${item.id}&url=${imageUrl+this.dataInfo.teamLogo}&name=${this.dataInfo.teamName}`
				})
			},
			coachList() {
				getCoachList({
					playerId: this.id,
					pageSize: 1000,
				}).then(res => {
					this.coachListData = res.rows;
				})
			},
			addplayer() {
				uni.navigateTo({
					url: `/pages/basketballTeam/addplayer?player=${this.dataInfo.id}&url=${this.dataInfo.teamLogo}&name=${this.dataInfo.teamName}`
				})
			},
			onDelecoach(item) {
				uni.showModal({
					content: '确定删除吗？',
					success: (res) => {
						if (res.confirm) {
							delcoach(item.id).then(res => {
								this.coachList();
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			delputInfo(item) {
				uni.showModal({
					content: '确定删除吗？',
					success: (res) => {
						if (res.confirm) {
							delbasketballPlayer(item.id).then(res => {
								this.basketballPlayerList();
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			basketballPlayerList(num) {
				this.loadingMore = true
				getbasketballPlayerList({
					playerId: this.id,
					pageSize: 9,
					pageNum: this.pageNum || 1,
				}).then(res => {
					if (res.code == 200) {
						if (res.rows.length < 9) {
							this.isLast = true
						}
						this.playerList.push(...res.rows)
					}
					this.loadingMore = false
				})
			}
		},
		onShow() {
			this.loadingMore = this.isLast = false
			this.pageNum = 1
			this.playerList = [];
			this.coachList();
			this.basketballPlayerList();
		},
		onLoad({
			id
		}) {
			this.id = id;
			getInfoBasketballTeam(id).then(res => {
				if (res.code == 200) {
					this.dataInfo = res.data;
				}
			})

		}
	}
</script>

<style scoped lang="scss">
	.header_box {
		display: flex;
		align-items: center;
		font-weight: 800;
		padding: 30rpx 20rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			margin-right: 20rpx;
			border-radius: 100%;
		}
	}

	.coachList {
		overflow: auto;

		::v-deep.uni-section__content-title {
			font-weight: 800;
		}

		.addcoach {
			background-color: $btn-bg-color;
			font-size: 24rpx;
			padding: 10rpx 10rpx;
			color: #fff;
			border-radius: 40rpx;
		}

		.coachListData {
			overflow: auto;
		}
	}

	.contentList {
		display: flex;
		padding: 30rpx;
		align-items: center;
		font-weight: 600;

		image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 100%;
			margin-right: 20rpx;
		}

		.num {
			display: flex;
			font-size: 24rpx;
			margin-top: 20rpx;
			padding: 10rpx;
			background: #E8F0FD;
		}
	}

	.contentBtn {
		display: flex;
		align-items: center;
		background-color: #F7F7F7;

		view {
			height: 20px;
			width: 1px;
			background-color: #E2E2E2;
		}

		button {
			width: calc(100% / 2);
			border: none;
			font-size: 28rpx;
			border-radius: 0;
			color: #7F7F7F;
			padding-top: 20rpx;
			padding-bottom: 20rpx;

			&::after {
				border: none;
			}
		}
	}
</style>